package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
 * @author Lucie
 *class Update_User_Controler
 *Class variables:msg,flag
 *Class methods:update_User,get_msg,set_msg
 */
public class Update_User_Controler  {
	
		private String msg; 
		private boolean flag;
		/**
		 * Constructor initializes the class variables
		 */
		public Update_User_Controler()
		{
			flag=true;
			msg=new String();
			msg=null;
		}
		
		/**
		 * 
		 * @return String white all of the problems that occurred in the class
		 */
		public String get_msg()
		{
			return msg;
		}
		
		/**
		*  @param new_msg
		* This method chain messages into a string ,
		*  the purpose ultimately is to display on the screen all the failures
		 */
		public void set_msg(String new_msg)
		{
			if (msg==null)
				msg=new_msg;
			else
			msg+=new_msg;
			
		}
	
		/**
		 * 
		 * @param user_id
		 * this  method call the class ;serverproxy, to Contact with the server,
		 *  the purpose of this method is to return all the user details
		 * first we check the Parameters we get (check_input) if something is wrong we return false 
		 * then we call "getUserdetails" that return all the details of a chosen user
		 * if something goes wrong we return null else we return the array list with all the user details
		 * @return ArrayList<String> with all the user details or null if something went wrong
		 */
	public ArrayList<String> get_user(String user_id)
		{
			 ArrayList<String> str_check=new  ArrayList<String>();
			 
		 
			 check_input(user_id);
			 	if (flag==false)
			 return null;
			 	
			 	str_check=BusinessImplements.GetInstance().getServerProxy().getUserdetails(user_id);
			 	if(str_check.get(0)==null)
			 	{
			 		set_msg("\nuser not exist"); 
			 		return null;
			 	}
				if(str_check.get(3).equals("1"))
			 	{
			 		set_msg("\nthe user is a memmber cant update hes deatails here\n go to update memmber "); 
			 		return null;
			 	}
			 return str_check;
		}
		
	 
	
	/**
	 * 
	 * @param user_id
	 * @param user_email
	 * @param user_passworld
	 * @param user_role
	* this  method call the class ;serverproxy, to Contact with the server,
		 *  the purpose of this method is to update user details of the chosen user
		 * first we check the Parameters we get (check_input) if something is wrong we return false 
		 * @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
	public boolean update_User(String user_id,String user_email,String user_passworld,String user_role)
		{
		
		 	ArrayList<String> str_user=new  ArrayList<String>();
	 
		 check_input( user_id,  user_email,user_passworld,user_role);
		 	if (flag==false)
		 return false;
		 
		 	str_user=BusinessImplements.GetInstance().getServerProxy().update_userdeatails( user_id,  user_email,user_passworld,user_role);
		 	if(str_user.get(0).equals("false"))
		 	{
		 		set_msg("\nuser all ready exist"); 
		 		return false;
		 	}
		 
			 	
		 	return flag;
	
}
		

/**
 * This method chain messages into a string ,
* the purpose of this method is to display on the screen all the failures
 * @param user_id
 */
			public void check_input(String user_id)
			{
				
				if(user_id.equals(""))
				{
					set_msg("\nmising meember id ");
					flag=false;
				}
			
				
			}
			/**
				 *This method chain messages into a string ,
				 * the purpose of this method is to display on the screen all the failures
				 * @param id
				 * @param email
				 * @param pass
				 * @param role
			*/
public void check_input(String id, String email,String pass,String role)
		{
				
				if(id.equals(""))
				{
					set_msg("\nmising user id ");
					flag=false;
				}
				if(email.equals(""))
				{
					set_msg("\nmising user email ");
					flag=false;
				}
				if(pass.equals(""))
				{
					set_msg("\nmising user passworld");
					flag=false;
				}
				if(role.equals(""))
				{
					set_msg("\nmising user role");
					flag=false;
				}
			
				
				
	}
		
		
		
	}



